<!--
 * @Descripttion: (AI提示内容维护/GB_AI_Prompt)
 * @Author: (admin)
 * @Date: (2025-06-20)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['gbaiprompt:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" v-hasPermi="['gbaiprompt:delete']" plain icon="delete" @click="handleClear">
          {{ $t('btn.clean') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['gbaiprompt:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      >
      <el-table-column prop="id" label="序号" align="center" v-if="columns.showColumn('id')"/>
      <el-table-column prop="promptType" label="指令类型" align="center" v-if="columns.showColumn('promptType')">
        <template #default="scope">
          <dict-tag :options=" options.promptTypeOptions" :value="scope.row.promptType"  />
        </template>
      </el-table-column>
      <el-table-column prop="prompt" label="指令" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('prompt')"/>
      <el-table-column prop="promptRemark" label="指令解释" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('promptRemark')"/>
      <el-table-column prop="temperature" label="随机性参数" align="center" v-if="columns.showColumn('temperature')"/>
      <el-table-column prop="eRPModel" label="所属ERP的模块" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('eRPModel')"/>
         <el-table-column prop="aI_KEY" label="AIKEY" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('aI_KEY')"/>
   <el-table-column prop="updateTime" label="更新时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('updateTime')"/>
      <el-table-column prop="updateName" label="更新人员" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('updateName')"/>
      <el-table-column prop="menunameKey" label="菜单" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('menunameKey')"/>
 
    <el-table-column label="操作" width="260">
        <template #default="scope">
          <el-button type="primary" size="small" icon="view" title="详情" @click="handlePreview(scope.row)"></el-button>
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['gbaiprompt:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['gbaiprompt:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />


    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
            

          <el-col :lg="12">
            <el-form-item label="指令类型" prop="promptType">
              <el-select v-model="form.promptType"  placeholder="请选择指令类型">
                <el-option
                  v-for="item in options.promptTypeOptions" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item label="指令" prop="prompt">
              <el-input type="textarea" v-model="form.prompt" placeholder="请输入指令"/>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="指令解释" prop="promptRemark">
              <el-input v-model="form.promptRemark" placeholder="请输入指令解释" />
            </el-form-item>
          </el-col>
    <el-col :lg="12">
            <el-form-item label="AI_KEY" prop="aI_KEY">
              <el-input v-model="form.aI_KEY" placeholder="AI_KEY" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="随机性参数" prop="temperature">
              <el-input v-model="form.temperature" placeholder="请输入降低随机性保证业务准确性 参数默认为 1.0,这里要0.0~1.0" />
            </el-form-item>
          </el-col>
 
          <el-col :lg="12">
            <el-form-item label="所属ERP的模块" prop="eRPModel">
              <el-input v-model="form.eRPModel" placeholder="请输入所属ERP的模块" />
            </el-form-item>
          </el-col> 
 
          <el-col :lg="12">
            <el-form-item label="菜单" prop="menunameKey">
              <el-input v-model="form.menunameKey" placeholder="请输入菜单" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" :loading="state.submitLoading" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="gbaiprompt">
import { listGbAiPrompt,
 addGbAiPrompt, delGbAiPrompt, 
 updateGbAiPrompt,getGbAiPrompt, 
 clearGbAiPrompt,  } 
from '@/api/buss/gbaiprompt.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'MenunameKey',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'id', label: '序号'   },
  { visible: true, align: 'center', type: 'dict', prop: 'promptType', label: '指令类型'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'prompt', label: '指令'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'promptRemark', label: '指令解释'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'aI_Key', label: 'AIKEY'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'temperature', label: '随参数'   },
  { visible: true, align: 'center', type: '', prop: 'eRPModel', label: '所属ERP的模块'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'updateTime', label: '更新时间'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'updateName', label: '更新人员'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'menunameKey', label: '菜单'  ,showOverflowTooltip: true  },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList(){
  loading.value = true
  listGbAiPrompt(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  submitLoading: false,
  form: {},
  rules: {
  },
  options: {
    // 指令类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
promptTypeOptions: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
    state.submitLoading = false
  form.value = {
    id: null,
    promptType: null,
    prompt: null,
    promptRemark: null,
    temperature: null,
    eRPModel: null,
    updateTime: null,
    updateName: null,
    menunameKey: null,
  };
  proxy.resetForm("formRef")
}

/**
 * 查看
 * @param {*} row
 */
function handlePreview(row) {
  reset()
    const id = row.id
    getGbAiPrompt(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '查看'
      opertype.value = 3
      form.value = {
        ...data,
      }
    }
  })
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  state.submitLoading = false
  title.value = '添加AI提示内容维护'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getGbAiPrompt(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改AI提示内容维护'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      state.submitLoading = true

      if (form.value.id != undefined && opertype.value === 2) {
        updateGbAiPrompt(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
            state.submitLoading = true

        })
      } else {
        addGbAiPrompt(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
          getList()
              state.submitLoading = true

          })
          .finally(() => {
            setTimeout(() => {
              state.submitLoading = false
            }, 800)
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delGbAiPrompt(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}

// 清空
function handleClear() {
  proxy
    .$confirm("是否确认清空所有数据项?", "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return clearGbAiPrompt()
    })
    .then(() => {
      handleQuery()
      proxy.$modal.msgSuccess('清空成功')
    })
}


// 导出按钮操作
function handleExport() {
  proxy
    .$confirm("是否确认导出AI提示内容维护数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      await proxy.downFile('/Buss/GbAiPrompt/export', { ...queryParams })
    })
}

handleQuery()
</script>